<template>
  <div>
    <el-dialog
      :title="titlecom"
      :visible.sync="dialogFormVisible"
      width="1300px"
    >
      <!-- :rules="rules" -->
      <el-form :model="form" label-width="100px" class="demo-ruleForm">
        <el-form-item
          label="分类"
          :label-width="formLabelWidth"
          prop="classifyid"
        >
          <el-select
            v-model="form.classifyid"
            placeholder="分类"
            class="specificationInput"
            filterable
          >
            <el-option
              v-for="a in reqCategory4"
              :key="a.id"
              :label="a.title"
              :value="a.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item
              label="省"
              :label-width="formLabelWidth"
              prop="category1Id"
              v-if="this.qubie == 0"
            >
              <!-- 通过:value和v-model收集item项商品的id -->
              <el-select
                v-model="form.category1Id"
                placeholder="请选择省"
                :disabled="isShowselect1"
              >
                <el-option
                  v-for="a in reqCategory1"
                  :key="a.id"
                  :label="a.name"
                  :value="a.id"
                ></el-option>
              </el-select> </el-form-item
            ></el-col>
          <el-col :span="6">
            <el-form-item label="市" prop="category2Id" v-if="this.qubie == 0">
              <el-select
                v-model="form.category2Id"
                placeholder="请选择市"
                :disabled="isShowselect2"
              >
                <el-option
                  v-for="item in reqCategory2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="区" prop="category3Id" v-if="this.qubie == 0">
              <el-select
                v-model="form.category3Id"
                placeholder="请选择区"
                :disabled="isShowselect3"
              >
                <el-option
                  v-for="c in reqCategory3"
                  :key="c.id"
                  :label="c.name"
                  :value="c.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="发布人id" :label-width="formLabelWidth" prop="uid">
          <el-input v-model="form.uid" class="specificationInput" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="发布人手机号" :label-width="formLabelWidth" prop="fbrphone">
          <el-input v-model="form.fbrphone" class="specificationInput"></el-input>
        </el-form-item>
        <el-form-item label="标题" :label-width="formLabelWidth" prop="title">
          <el-input v-model="form.title" class="specificationInput"></el-input>
        </el-form-item>
        <el-form-item label="需求数量" :label-width="formLabelWidth" prop="num">
          <el-input v-model="form.num" class="specificationInput"></el-input>
        </el-form-item>
        <el-form-item label="线索价格" :label-width="formLabelWidth" prop="price">
          <el-input v-model="form.price" class="specificationInput"></el-input>
        </el-form-item>
        <el-form-item label="发布人名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" class="specificationInput"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" :label-width="formLabelWidth" prop="phone">
          <el-input v-model="form.phone" class="specificationInput"></el-input>
        </el-form-item>

        <el-form-item
          label="文案"
          prop="content"
          :label-width="formLabelWidth"
          style="margin-top: 30px"
        >
          <!-- <editor-bar
            v-model="form.content"
            :isClear="isClear"
            @change="change"
            style="width: 1000px; margin: 0"
          >
          </editor-bar> -->
          <el-input
            type="textarea"
            :rows="4"
            style="width: 547px"
            placeholder="请输入描述"
            v-model="form.content"
          >
          </el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="quedingtianjia()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addcopywritingadd,
  updatecopywritingupdate,
  gettypelist,
  getcitylist,
} from "@/api/servetypeIndex";
import { upload } from "@/api/editor.js";
import EditorBar from "@/components/Editor";
import draggable from "vuedraggable";
export default {
  components: {
    EditorBar,
    draggable,
  },
  data() {
    return {
      reqCategory4: [],
      reqCategory1: [],
      // 市
      reqCategory2: [],
      // 区
      reqCategory3: [],
      isClear: false,
      dialogVisible: false,
      dialogFormVisible: false,
      formLabelWidth: "180px",
      form: {
        category1Id: "",
        category2Id: "",
        category3Id: "",
        classifyid: "",
        fbrphone: "",
        title: "",
        content: "",
        num: "",
        price: "",
        name: "",
        phone: "",
      },
      dialogImageUrl: "",
      dialogVideoUrl: "",
      dialogVisible1: false,
      dialogVisible2: false,
      dialogVisiblevideo: false,
      rules: {
        classifyid: [
          { required: true, message: "请选择分类", trigger: "blur" },
        ],
        category1Id: [{ required: true, message: "请选择省", trigger: "blur" }],
        category2Id: [{ required: true, message: "请选择市", trigger: "blur" }],
        category3Id: [
          {
            required: true,
            message: "请选择区",
            trigger: "blur",
          },
        ],
      },
      qubie: 0,
    };
  },
  methods: {
    changeselect(val) {
      // console.log(val);
      this.form.servetypeid = val.id;
      this.form.servetype = val.name;
    },
    change() {},
    async gettypelist() {
      let result = await gettypelist({adminid:Number(localStorage.getItem('userId'))});
      console.log(result);
      if (result.code == 0) {
        this.reqCategory4 = result.data;
      }
    },
    async getcitylist1() {
      let result = await getcitylist({adminid:Number(localStorage.getItem('userId'))});
      console.log(result);
      if (result.code == 0) {
        this.reqCategory1 = result.data;
      }
    },
    // 将富文本获取回来数据上剔除掉html标签
    getSimpleText(html) {
      var re1 = new RegExp("<.+?>", "g"); //匹配html标签的正则表达式，"g"是搜索匹配多个符合的内容
      var msg = html.replace(re1, ""); //执行替换成空字符
      return msg;
    },
    add() {
      this.form = {
        category1Id: "",
        category2Id: "",
        category3Id: "",
        classifyid: "",
        fbrphone: "",
        title: "",
        content: "",
        num: "",
        price: "",
        name: "",
        phone: "",
      };
      this.qubie = 0;
      this.gettypelist();
      this.getcitylist1();
      this.dialogFormVisible = true;
    },
    edit(val) {
      console.log("点击了编辑", val);
      this.form = {
        category1Id: "",
        category2Id: "",
        category3Id: "",
        classifyid: "",
        fbrphone: "",
        title: "",
        content: "",
        num: "",
        price: "",
        name: "",
        phone: "",
      };
      this.qubie = 1;
      this.imgList = [];
      console.log("图片", val.images);
      this.gettypelist();
      this.getcitylist1();
      this.form = Object.assign(this.form, val);
      this.dialogFormVisible = true;
    },
    // 关闭
    handleClose() {
      this.dialogFormVisible = false;
      this.$emit("refresh");
    },

    async quedingtianjia() {
      if (this.qubie == 0) {
        let param = {
          classifyid: parseInt(this.form.classifyid),
          provinceid: parseInt(this.form.category1Id),
          cityid: parseInt(this.form.category2Id),
          countyid: parseInt(this.form.category3Id),
          fbrphone: this.form.fbrphone,
          title: this.form.title,
          content: this.form.content,
          name: this.form.name,
          phone: this.form.phone,
          num: parseInt(this.form.num),
          price: parseFloat(this.form.price),
        };
        console.log(param);
        let result = await addcopywritingadd(param);
        if (result.code == 0) {
          this.$message({
            message: `添加成功`,
            type: "success",
          });
          this.handleClose();
        }
      } else if (this.qubie == 1) {
        let param = {
          id: parseInt(this.form.id),
          classifyid: parseInt(this.form.classifyid),
          provinceid: parseInt(this.form.category1Id),
          cityid: parseInt(this.form.category2Id),
          countyid: parseInt(this.form.category3Id),
          fbrphone: this.form.fbrphone,
          title: this.form.title,
          content: this.form.content,
          name: this.form.name,
          phone: this.form.phone,
          num: parseInt(this.form.num),
          price: parseFloat(this.form.price),
        };
        let result = await updatecopywritingupdate(param);
        if (result.code == 0) {
          this.$message({
            message: `修改成功`,
            type: "success",
          });
          this.handleClose();
        }
      }
    },
  },
  watch: {
    "form.category1Id": {
      handler(newVal) {
        if (newVal) {
          // console.log(newVal);
          // console.log(this.reqCategory1);

          let arr = this.reqCategory1.filter((item) => item.id == newVal);
          // console.log(arr);
          this.reqCategory2 = arr[0].List;

          this.form.category2Id = "";
          this.form.category3Id = "";
          this.form.cityid = newVal;

          this.getcitylist1();
        }
      },
      deep: true,
      // immediate: true,
    },
    "form.category2Id": {
      handler(newVal) {
        console.log(newVal);
        if (newVal) {
          let arr = this.reqCategory2.filter((item) => item.id == newVal);
          this.reqCategory3 = arr[0].List;
          console.log("啊啊啊啊啊啊啊啊啊", this.reqCategory3);

          // console.log(newVal);
          this.form.category3Id = "";
          this.form.cityid = newVal;
          this.getcitylist1();
        }
      },
      deep: true,
      // immediate: true,
    },
    "form.category3Id": {
      handler(newVal) {
        if (newVal) {
          this.form.cityid = newVal;
          this.getcitylist1();
        }
      },
      deep: true,
      // immediate: true,
    },
    imgList: {
      handler(newVal) {
        // console.log("监听", newVal);
        // 这里创建新的数组 用逗号进行隔开 主要是接口要求
        let imagesarr = [];
        const result = newVal.map((item) => `${item.url}`).join(",");
        this.form.images = result;
        console.log("猪嗨", this.form.images);
      },
      deep: true,
    },
  },
  computed: {
    titlecom() {
      if (this.qubie == 0) {
        return "添加文案";
      } else {
        return "修改文案";
      }
    },
  },
};
</script>

<style scoped lang="scss">
.specificationInput {
  width: 250px;
}
.avatar-uploader-icon[data-v-22a48a68] {
  border: 1px dashed #d9d9d9;
  width: 300px;
  height: 300px;
  display: flex;
  justify-content: center;
  flex-direction: column;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  width: 300px;
  height: 300px;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.addguigeBtn {
  margin-left: 100px;
  margin-bottom: 22px;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 180px;
  height: 180px;
  line-height: 180px;
  text-align: center;
}

.avatar {
  width: 180px;
  height: 180px;
  display: block;
}
v-deep .el-upload--picture-card {
  width: 300px !important;
  height: 300px !important;
}
.specificationInput1 {
  width: 150px;
}
</style>
